import { bannerlist,bannercount } from "@/request/api";
const state = {
    bannerList : [],
    page:1,
    size:5,
    count:0
};
const getters = {
    getBanner(state){
        return state.bannerList
    },
    getSize(state){
        return state.size
    },
    getCount(state){
        return state.count
    }
};
const mutations = {
    REQ_BANNERLIST(state,payload){
        state.bannerList = payload
    },
    REQ_COUNT(state,payload){
        state.count = payload
    },
    CHANGE_PAGE(state,payload){
        state.page = payload
    },
};
const actions = {
    getBannerListAction(context){
        bannerlist({
            page:context.state.page,
            size:context.state.size
        }).then(res=>{
            if (res.code == 200) {
                context.commit('REQ_BANNERLIST',res.list)
                let list = res.list ? res.list : []
                if (context.state.page != 1 && list.length == 0) {
                    context.dispatch('changePageAction',context.state.page-1)
                }
            }
        })
    },
    getCountAction({commit}){
        bannercount().then(res=>{
            if (res.code == 200) {
                commit('REQ_COUNT',res.list[0],total)
            }
        })
    },
    changePageAction(context,payload){
        context.commit('CHANGE_PAGE',payload);
        context.dispatch('getBannerListAction')
    }
};
export default {
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}